package com.glacier.common.core.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.glacier.common.core.constant.DictEnum;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * date 2022-11-04 16:15
 * 基本实体
 *
 * @author glacier
 * @version 1.0
 */
public abstract class BaseEntity implements Expandable {
	private static final long serialVersionUID = 5426093680677849722L;

	/**
	 * 主键
	 */
	protected String id;

	/**
	 * 查询参数
	 * scope 数据权限
	 * current 当前用户
	 */
	private final Map<String, Object> params = new HashMap<>(8);

	/**
	 * 创建者
	 */
	protected String createBy;

	/**
	 * 创建时间
	 */
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	protected LocalDateTime createTime;

	/**
	 * 更新者
	 */
	protected String updateBy;

	/**
	 * 更新时间
	 */
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	protected LocalDateTime updateTime;

	/**
	 * 备注
	 */
	protected String remark;

	/**
	 * 是否是新记录（默认：false）
	 */
	private boolean newRecordFlag = false;
	/**
	 * 删除标记(0：正常(默认)；1：删除；)
	 */
	private String delFlag = DictEnum.DATA_NORMAL.getCode();

	protected BaseEntity() {
	}

	protected BaseEntity(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	/**
	 * 判断为新记录 newRecordFlag 为 true 且 id不为空
	 * @return true 新记录 false 更新
	 */
	@JsonIgnore
	public boolean isNewRecordFlag() {
		return newRecordFlag || id == null || id.isBlank();
	}

	public void setNewRecordFlag(boolean newRecordFlag) {
		this.newRecordFlag = newRecordFlag;
	}

	@Override
	@JsonIgnore
	public Map<String, Object> getParams() {
		return params;
	}

	public String getCreateBy() {
		return createBy;
	}

	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	public LocalDateTime getCreateTime() {
		return createTime;
	}

	public void setCreateTime(LocalDateTime createTime) {
		this.createTime = createTime;
	}

	public String getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(String updateBy) {
		this.updateBy = updateBy;
	}

	public LocalDateTime getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(LocalDateTime updateTime) {
		this.updateTime = updateTime;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}

	@Override
	public String toString() {
		return "BaseEntity{" +
				"id='" + id + '\'' +
				", params=" + params +
				", createBy='" + createBy + '\'' +
				", createTime=" + createTime +
				", updateBy='" + updateBy + '\'' +
				", updateTime=" + updateTime +
				", remark='" + remark + '\'' +
				", newRecordFlag=" + newRecordFlag +
				", delFlag='" + delFlag + '\'' +
				'}';
	}
}
